package section6_2;

import java.util.concurrent.ConcurrentLinkedDeque;

/**
 * Created by Hyman on 2017/6/12.
 *
 * 使用非阻塞式线程安全列表 ConcurrentLinkedDeque
 */
public class Main {

    public static void main(String[] args) {

        ConcurrentLinkedDeque<String> list = new ConcurrentLinkedDeque<>();
        Thread[] threads = new Thread[100];

        // 创建100个AddTask对象，并且让他们在各自的线程中运行
        for(int i=0; i<threads.length; i++) {
            AddTask task = new AddTask(list);
            threads[i] = new Thread(task);
            threads[i].start();
        }

        System.out.printf("Main: %d AddTask threads have been launched\n", threads.length);

        for(int i=0; i<threads.length; i++) {
            try {
                threads[i].join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        // 输出队列长度信息
        System.out.printf("Main: Size of the List: %d\n", list.size());

        // 创建100个PollTask对象，并且让他们在各自的线程中运行
        for(int i=0; i<threads.length; i++) {
            PoolTask task = new PoolTask(list);
            threads[i] = new Thread(task);
            threads[i].start();
        }

        System.out.printf("Main: %d PollTask threads have been launched\n", threads.length);

        // 等待线程执行完
        for(Thread thread : threads) {
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        // 输出队列长度信息
        System.out.printf("Main: Size of the List: %d\n", list.size());

    }

}
